package com.eshop.eshopmanagersystem.dal;

import com.eshop.eshopmanagersystem.idal.IEchartsDAL;
import com.eshop.eshopmanagersystem.model.EchartsInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 报表接口数据实现层
 *
 */
@Service
public class EchartsDAL {

    @Autowired
    IEchartsDAL iEchartsDAL;
    /**
     * 统计最近7天的数据并按天分组
     *
     */
    private List<EchartsInfo> getEchartTableBySevenDays(){
        return iEchartsDAL.getEchartTableBySevenDays();
    }

    /**
     * 数据构造与封装
     */
    public HashMap<String, Object> getEchartsData(){
        //新建HashMap数据模型用来装载返回给前端echarts所用数据格式
        HashMap<String, Object> hs = new HashMap<>();
        //读取数据库查询结果集
        List<EchartsInfo> echartsList = getEchartTableBySevenDays();
        //构建销售金额数据数组
        List<String> priceList = new ArrayList<>();
        //构建日期数据数组
        List<String> daysList = new ArrayList<>();
        //构建销售金额与日期数据数组
        List<EchartsInfo> sumList = new ArrayList<>();
        for(EchartsInfo item:echartsList){
            //装载价格报表数据
            priceList.add(item.getTotal_price());
            //装载日期报表数据
            daysList.add(item.getDays());
            //装载数量报表数据
            EchartsInfo newChartsItem = new EchartsInfo();
            newChartsItem.setSum(item.getSum());
            newChartsItem.set销售数量(item.getSum());
            newChartsItem.setDays(item.getDays());
            sumList.add(newChartsItem);
        }
        hs.put("totalPriceList",priceList);
        hs.put("days",daysList);
        hs.put("sum",sumList);
        return hs;
    }

}
